package com.itheima.app.manager;

import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Acoustic;
import com.itheima.domain.db.UserInfo;

import com.itheima.service.db.AcousticService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.AcousticVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@Component
public class AcousticManger {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    @DubboReference
    private AcousticService acousticService;


    //桃花传音
    public void sendAcoustic(MultipartFile multipartFile) throws IOException {
        //   StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FileUtil.extName(new File(multipartFile.getOriginalFilename())), null);
        StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FileUtil.extName(new File(multipartFile.getOriginalFilename())), null);
        //生成连接
        String webServerUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
        Long id = UserHolder.get().getId();


        //rpc
        acousticService.sendAcoustic(id, webServerUrl);

    }

    @DubboReference
    private UserInfoService userInfoService;
//
//
//    //接收
//    public ResponseEntity findAcoustic() {
//        //获取用户的id
//        Long userId = UserHolder.get().getId();
//        //获取用户的次数
//
//        UserInfo userInfo = userInfoService.findById(userId);
//        Integer count = userInfo.getNumber();
//
//
//        //调用 rpc  查询 其他人发送发 语音  排除自己 没有拾取的  并且自由一条
//        Acoustic acoustic = acousticService.findAcoustic(userId);
//
//        if (count != 0 && acoustic != null) {
//            UserInfo MyuserInfo = userInfoService.findById(userId);
//            MyuserInfo.setNumber(count - 1);
//            userInfoService.update(MyuserInfo);
//            //已近被拾取
//            acoustic.setPickup(1);
//            acousticService.update(acoustic);
//
//        }
//
//        if (acoustic != null) {
//
//            //获取 推荐给我 的用户的 信息
//            UserInfo userInfoServiceById = userInfoService.findById(acoustic.getUserId());
//
//            AcousticVo acousticVo = new AcousticVo();
//            acousticVo.setUserInfo(userInfoServiceById, acoustic.getAddress(), count);
//
//
//            //风住哪个vo
//            return ResponseEntity.ok(acousticVo);
//
//        }
//
//        return ResponseEntity.status(500).body("没有漂流瓶了");
//
//
//    }

    //接收
    public ResponseEntity findAcoustic() {

        //获取线程id 查询 今天拾取的次数
        Long userId = UserHolder.get().getId();
        Integer count = acousticService.findCount(userId);
        count=count + 1;
        //查询出来 不是自己 发送的
        Acoustic acoustic = acousticService.findAcoustic(userId);

        AcousticVo acousticVo;
            if (count <= 5&&acoustic!=null) {
            //查询未拾取的  排除自己

            if (acoustic != null) {

                    count = 5-count;



                //获取 推荐给我 的用户的 信息
                UserInfo userInfoServiceById = userInfoService.findById(acoustic.getUserId());
                acousticVo = new AcousticVo();
                acousticVo.setUserInfo(userInfoServiceById, acoustic.getAddress(), count);
                //修改该条信息
                acoustic.setPickup(1);
                acoustic.setPickupUserId(userId);
                acoustic.setPickupDateTime(new Date());
                acousticService.update(acoustic);

                //返回
                return ResponseEntity.ok(acousticVo);


            }


        } else if (count>5){
            count = 0;
            UserInfo userInfoServiceById = userInfoService.findById(acoustic.getUserId());
            acousticVo = new AcousticVo();
            acousticVo.setUserInfo(userInfoServiceById, acoustic.getAddress(), 0);
            return ResponseEntity.ok(acousticVo);
        }

            return ResponseEntity.status(500).body("没有漂流瓶了");


    }


}
